package com.scau.clothes.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.scau.clothes.dao.ClothesDao;
import com.scau.clothes.pojo.Clothes;
import com.scau.clothes.pojo.Color;
import com.scau.clothes.service.ClothesService;
import com.scau.clothes.service.ColorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service("clothesService")
public class ClothesServiceImpl implements ClothesService {
    @Autowired
    ClothesDao clothesDao;
    @Autowired
    ColorService colorService;
    @Autowired
    StringRedisTemplate redisTemplate;
    //hash
    private static final String CLOTHES = "CLOTHES";
    //zset
    private static final String HOT_SALE_CLOTHES = "HOT_SALE_CLOTHES";
    private static final String CLOTHES_ID = "CLOTHES_ID";
    private static final int pageSize = 5;
    private int totalClothes;
    /**
     * 从redis中获取
     * @param id
     * @return
     */
    private Clothes getClothesFromRedisById(int id){
        String clothesJSON = ((String) redisTemplate.opsForHash().get(CLOTHES, CLOTHES + id));
        Clothes clothes = JSONObject.parseObject(clothesJSON, Clothes.class);
        return clothes;
    }
    /**
     * 从mysql中获取
     * @param id
     * @return
     */
    private Clothes getClothesFromMysqlById(int id){
        Clothes clothes = clothesDao.getClothesById(id);
        return clothes;
    }
    /**
     * 5.4晚点再搞
     * @param colorVal
     * @param size
     * @param type
     * @param area
     */
    private void putAddClothesToRedisByParas(int colorVal, int size, int type, int area){
        int temp = 1;
        while(temp<=colorVal){
            if((temp&colorVal)==0)
                continue;
            Color color = colorService.getColorByVal(temp);

        }
    }

    @Override
    public List<Clothes> getClothesByPage(int curPage, int type,int sortWay) {
        LinkedList<Clothes> resList = new LinkedList<>();
        Set<String> resSet = null;
        String typKey = "";
        if (type == 0)
            typKey = CLOTHES_ID;
        else if(type == 1)
            typKey = HOT_SALE_CLOTHES;
        long startIdx = (curPage-1)*pageSize;
        if(sortWay==0)
            resSet = redisTemplate.opsForZSet().range(typKey,startIdx,startIdx+pageSize);
        else
            resSet = redisTemplate.opsForZSet().reverseRange(typKey,startIdx,startIdx+pageSize);
        for (String s : resSet){
            String clothesJSON = (String) redisTemplate.opsForHash().get(CLOTHES, s);
            resList.add(JSONObject.parseObject(clothesJSON,Clothes.class));
        }

        return resList;
    }

    @Override
    public void initData() {
        List<Clothes> list = clothesDao.getAllClothes();
        for (Clothes clothes : list) {
            String clothesJSON = JSONObject.toJSONString(clothes);
            redisTemplate.opsForHash().putIfAbsent(CLOTHES,CLOTHES+clothes.getId(),clothesJSON);
            redisTemplate.opsForZSet().add(CLOTHES_ID,CLOTHES+clothes.getId(),clothes.getId());
        }
    }

    @Override
    public int getTotalClothes() {
        return 0;
    }

    @Override
    public List<Clothes> getHotSale(int offset,int count) {
        Set<String> cIdSet = redisTemplate.opsForZSet().rangeByScore(HOT_SALE_CLOTHES, 0, -1, offset, count);
        List<Clothes> res = new LinkedList<>();
        for (String cId : cIdSet) {
            Clothes clothes = getClothesById(Integer.parseInt(cId.trim()));
            res.add(clothes);
        }
        return res;
    }
    @Override
    public void updateHotSale(int clothesId, int num) {
        redisTemplate.opsForZSet().incrementScore(HOT_SALE_CLOTHES,CLOTHES+clothesId,num);
    }

    @Override
    public Clothes getClothesById(int id) {
        Clothes clothes = getClothesFromRedisById(id);
        if(clothes!=null){
            redisTemplate.opsForZSet().add(CLOTHES_ID,CLOTHES+id,id);
        }else{
            clothes = getClothesFromMysqlById(id);
            String clothesJSON = JSONObject.toJSONString(clothes);
            //防止缓存穿透
            redisTemplate.opsForHash().put(CLOTHES,CLOTHES+id,clothesJSON);
            if(clothes!=null)
                redisTemplate.opsForZSet().add(CLOTHES_ID,CLOTHES+id,id);
        }
        return clothes;
    }

    @Override
    public List<Clothes> getClothesByByParas(int color, int size, int type, int area) {
        return clothesDao.getClothesByParas(color,size,type,area);
    }

    @Override
    public Clothes getPriceByClothesId(int id) {
        Clothes clothes = clothesDao.getPriceByClothesId(id);
        return clothes;
    }

    @Override
    public BigDecimal getActualPriceByClothesId(int id) {
        Clothes clothes = clothesDao.getPriceByClothesId(id);
        if (clothes.getPromotionStatus()==0) {
            return clothes.getPrice();
        }else{
            return clothes.getPromotionPrice();
        }
    }
}
